package school.valon.rest.services;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import school.valon.business.CourseBusiness;
import school.valon.dto.CourseDto;
import school.valon.dto.CourseOfferingDto;
import school.valon.dto.CourseOfferingStudentEnrolmentDto;
import school.valon.dto.StudentClassRecordingCommentDto;
import school.valon.dto.StudentClassRecordingDto;

@Path("/courses")
@PermitAll
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public class CourseService {
	@Autowired()
	@Qualifier("courseBusiness")
	private CourseBusiness courseBusiness;

	@POST
	@RolesAllowed({"admin"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createCourse(CourseDto courseDto) throws Exception {
		courseDto = courseBusiness.saveCourse(courseDto);
		return Response.status(Response.Status.CREATED).entity(courseDto).build();
	}

	@PUT
	@Path("{courseId}")
	@RolesAllowed({"admin"})
	public Response updateCourse(@PathParam("courseId") String courseId, CourseDto courseDto) throws Exception {
		courseDto.setCourseId(Integer.valueOf(courseId));
		courseBusiness.saveCourse(courseDto);
		return Response.ok("Course updated").build();
	}

	@DELETE
	@Path("{courseId}")
	@RolesAllowed({"admin"})
	public Response deleteCourse(@PathParam("courseId") String courseId) throws Exception {
		courseBusiness.deleteCourse(Integer.valueOf(courseId));
		return Response.ok("Course deleted").build();
	}

	@POST
	@Path("offerings")
	@RolesAllowed({"admin"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createCourseOffering(CourseOfferingDto courseOfferingDto) throws Exception {
		return Response.status(Response.Status.CREATED).entity(
				courseBusiness.saveCourseOffering(courseOfferingDto)).build();
	}

	@PUT
	@Path("offerings/{courseOfferingId}")
	@RolesAllowed({"admin"})
	public Response updateCourseOffering(@PathParam("courseOfferingId") String courseOfferingId,
			CourseOfferingDto courseOfferingDto) throws Exception {
		courseOfferingDto.setCourseOfferingId(Integer.valueOf(courseOfferingId));
		courseOfferingDto = courseBusiness.saveCourseOffering(courseOfferingDto);
		return Response.ok("Course offering updated").build();
	}

	@DELETE
	@Path("offerings/{courseOfferingId}")
	@RolesAllowed({"admin"})
	public Response deleteCourseOffering(@PathParam("courseOfferingId") String courseOfferingId) throws Exception {
		courseBusiness.deleteCourseOffering(Integer.valueOf(courseOfferingId));
		return Response.ok("Course offering deleted").build();
	}

	@POST
	@Path("studentClassRecordings")
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createStudentClassRecording(StudentClassRecordingDto studentClassRecordingDto) throws Exception {
		studentClassRecordingDto = courseBusiness.saveStudentClassRecording(studentClassRecordingDto);
		return Response.status(Response.Status.CREATED).entity(studentClassRecordingDto).build();
	}

	@PUT
	@Path("studentClassRecordings/{stdClassRecId}")
	@RolesAllowed("staff")
	public Response updateStudentClassRecording(@PathParam("stdClassRecId") String stdClassRecId,
			StudentClassRecordingDto studentClassRecordingDto) throws Exception {
		studentClassRecordingDto.setStudentClassRecordingId(Integer.valueOf(stdClassRecId));
		studentClassRecordingDto = courseBusiness.saveStudentClassRecording(studentClassRecordingDto);
		return Response.ok("Student class recording updated").build();
	}

	@DELETE
	@Path("studentClassRecordings/{stdClassRecId}")
	@RolesAllowed("staff")
	public Response deleteStudentClassRecording(@PathParam("stdClassRecId") String stdClassRecId) throws Exception {
		courseBusiness.deleteStudentClassRecording(Integer.valueOf(stdClassRecId));
		return Response.ok("Student class recording deleted").build();
	}

	@POST
	@Path("studentClassRecCommentsFromStaff")
	@RolesAllowed({"staff"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createStudentClassRecordingStaffComment(StudentClassRecordingCommentDto studentClassRecordingCommentDto) throws Exception {
		studentClassRecordingCommentDto = courseBusiness.saveStudentClassRecordingComment(studentClassRecordingCommentDto);
		return Response.status(Response.Status.CREATED).entity(studentClassRecordingCommentDto).build();
	}

	@POST
	@Path("studentClassRecCommentsFromParent")
	@RolesAllowed({"parent"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createStudentClassRecordingParentComment(StudentClassRecordingCommentDto studentClassRecordingCommentDto) throws Exception {
		studentClassRecordingCommentDto = courseBusiness.saveStudentClassRecordingComment(studentClassRecordingCommentDto);
		return Response.status(Response.Status.CREATED).entity(studentClassRecordingCommentDto).build();
	}

	@POST
	@Path("offerings/studentEnrolments")
	@RolesAllowed({"student"})
	@Produces(MediaType.APPLICATION_JSON)
	public Response createOfferingStudentEnrolment(CourseOfferingStudentEnrolmentDto courseOfferingStudentEnrolmentDto) throws Exception {
		courseOfferingStudentEnrolmentDto = courseBusiness.saveCourseOfferingStudentEnrolment(courseOfferingStudentEnrolmentDto);
		return Response.status(Response.Status.CREATED).entity(courseOfferingStudentEnrolmentDto).build();
	}
	
}
